home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Applications / Netrix / Class.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  11.4 KB  |  458 lines

  1. /*=====================================================================
  2.   Project:   Netrix.bdsproj
  3.  
  4.   File:      Class.cs
  5.  
  6.   Summary:   By recreating a popular game, this simple project
  7.              demonstrates how to use System.Drawing capabilities
  8.              of .NET Framework, and how to handle keyboard key
  9.              strokes in your .NET application.
  10.  
  11. ---------------------------------------------------------------------
  12.      Example submitted by Alfred Mirzagitov
  13.  
  14. =====================================================================*/
  15.  
  16. using System;
  17. using System.Drawing;
  18. using System.Windows.Forms;
  19.  
  20. namespace Netrix
  21. {
  22.   /// <summary>
  23.   /// Summary description for Class.
  24.   /// </summary>
  25.  
  26.   public enum MyColors
  27.   {
  28.     Red     = 1,
  29.     Blue    = 2,
  30.     Orange  = 3,
  31.     Yellow  = 4,
  32.     Lime    = 5,
  33.     Aqua    = 6,
  34.     Magenta = 7,
  35.     Black   = 8
  36.   }
  37.  
  38.   public class PlayingField: System.Windows.Forms.Panel
  39.   {
  40.     public int[ , ] pfmatrix; //col, row
  41.  
  42.     private WinForm FOwner;
  43.     private GamePiece gp;
  44.     private Timer MainTimer;
  45.     private int RowsRemoved = 0;
  46.     private int CurrentDelay = 500;
  47.  
  48.     public int FieldWidth = 10;
  49.     public int FieldHeight = 20;
  50.  
  51.     public PlayingField(WinForm aOwner, int x, int y, int h, int w)
  52.     {
  53.       FOwner = aOwner;
  54.  
  55.       this.Parent = FOwner;
  56.       this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  57.                     | System.Windows.Forms.AnchorStyles.Right)));
  58.       this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  59.       this.Location = new System.Drawing.Point(x, y);
  60.       this.Name = "MainPanel";
  61.       this.Size = new System.Drawing.Size(h, w);
  62.       this.TabIndex = 3;
  63.  
  64.       pfmatrix = new int[FieldWidth, FieldHeight];
  65.       EmptyPlayingField();
  66.       gp = new GamePiece(this);
  67.  
  68.       MainTimer = new Timer();
  69.       MainTimer.Tick += new System.EventHandler(this.TimerEvent);
  70.     }
  71.  
  72.     protected override void OnPaint(PaintEventArgs e)
  73.     {
  74.       Graphics g = e.Graphics;
  75.  
  76.       SuspendLayout();
  77.  
  78.       SolidBrush b = new SolidBrush(Color.FromArgb(180, Color.White));
  79.  
  80.       int x, hor_offset, ver_offset, current;
  81.       if ((Width / FieldWidth) < (Height / FieldHeight))
  82.         x = Width / FieldWidth;
  83.       else
  84.         x = Height / FieldHeight;
  85.  
  86.       hor_offset = (Width - x * FieldWidth) / 2;
  87.       ver_offset = (Height - x * FieldHeight) / 2;
  88.  
  89.       Pen p = new Pen(Color.Gray);
  90.       g.DrawLine(p, hor_offset,
  91.                     ver_offset,
  92.                     hor_offset,
  93.                     ver_offset+x * FieldHeight);
  94.       g.DrawLine(p, hor_offset,
  95.                     ver_offset+x * FieldHeight,
  96.                     hor_offset+x * FieldWidth,
  97.                     ver_offset+x * FieldHeight);
  98.       g.DrawLine(p, hor_offset+x * FieldWidth,
  99.                     ver_offset+x * FieldHeight,
  100.                     hor_offset+x * FieldWidth,
  101.                     ver_offset);
  102.  
  103.       x = x - 1;
  104.  
  105.       for (int r = 0; r < FieldHeight; r++)
  106.         for (int c = 0; c < FieldWidth; c++) {
  107.           current = pfmatrix[c,r];
  108.  
  109.           if (gp != null) {
  110.             if ((gp.row <= r) && (r < gp.row+gp.cMaxRows) &&
  111.                 (gp.col <= c) && (c < gp.col+gp.cMaxCols))
  112.                 current |= gp.cPiece[c-gp.col,r-gp.row];
  113.           }
  114.  
  115.           switch (current) {
  116.             case (int)MyColors.Red:     b.Color = Color.Red;     break;
  117.             case (int)MyColors.Lime:    b.Color = Color.Lime;    break;
  118.             case (int)MyColors.Orange:  b.Color = Color.Orange;  break;
  119.             case (int)MyColors.Blue:    b.Color = Color.Blue;    break;
  120.             case (int)MyColors.Yellow:  b.Color = Color.Yellow;  break;
  121.             case (int)MyColors.Black:   b.Color = Color.Black;   break;
  122.             case (int)MyColors.Magenta: b.Color = Color.Magenta; break;
  123.             case (int)MyColors.Aqua:    b.Color = Color.Aqua;    break;
  124.             default:                    b.Color = BackColor;     break;
  125.           }
  126.           g.FillRectangle(b, hor_offset+1+c*(x+1), ver_offset+1+(FieldHeight-1-r)*(x+1),x,x);
  127.         }
  128.       ResumeLayout(false);
  129.     }
  130.  
  131.     public void EmptyPlayingField()
  132.     {
  133.       int row, col;
  134.  
  135.       for (row = 0; row < FieldHeight; row++)
  136.         for (col = 0; col < FieldWidth; col++)
  137.           pfmatrix[col,row] = 0;
  138.     }
  139.  
  140.     private void SpeedUp(int r)
  141.     {
  142.       if (r > 10) return;
  143.       RowsRemoved++;
  144.       if ((RowsRemoved > 35) && (CurrentDelay > 450))
  145.         CurrentDelay -= 50;
  146.       if ((RowsRemoved > 55) && (CurrentDelay > 400))
  147.         CurrentDelay -= 50;
  148.       if ((RowsRemoved > 75) && (CurrentDelay > 350))
  149.         CurrentDelay -= 50;
  150.       if ((RowsRemoved > 85) && (CurrentDelay > 300))
  151.         CurrentDelay -= 50;
  152.       if ((RowsRemoved > 90) && (CurrentDelay > 250))
  153.         CurrentDelay -= 50;
  154.       if ((RowsRemoved > 95) && (CurrentDelay > 200))
  155.         CurrentDelay -= 50;
  156.       if ((RowsRemoved > 100) && (CurrentDelay > 200))
  157.         CurrentDelay -= 50;
  158.     }
  159.  
  160.     private void RemoveRow(int r)
  161.     {
  162.       int row, col;
  163.  
  164.       SpeedUp(r);
  165.       FOwner.AddScore(10*(r+1));
  166.  
  167.       for (row = r; row < FieldHeight-1; row++)
  168.         for (col = 0; col < FieldWidth; col++)
  169.           pfmatrix[col,row] = pfmatrix[col,row+1];
  170.  
  171.       for (col = 0; col < FieldWidth; col++)
  172.         pfmatrix[col,FieldHeight-1] = 0;
  173.     }
  174.  
  175.     public void RemoveRows(int score)
  176.     {
  177.       bool hole;
  178.       int c,r = 0;
  179.  
  180.       FOwner.AddScore(score);
  181.       do {
  182.         hole = false;
  183.         for (c = 0; c < FieldWidth; c++)
  184.           if (pfmatrix[c,r] == 0) hole = true;
  185.         if (hole)
  186.           r++;
  187.         else
  188.           RemoveRow(r);
  189.       } while (r < FieldHeight);
  190.     }
  191.  
  192.     public void GoLeft()
  193.     {
  194.       if (gp != null) gp.StepLeft();
  195.     }
  196.  
  197.     public void GoRight()
  198.     {
  199.       if (gp != null) gp.StepRight();
  200.     }
  201.  
  202.     public void TurnClockwise()
  203.     {
  204.       if (gp != null) gp.Rotate(true);
  205.     }
  206.  
  207.     public void TurnCounterclockwise()
  208.     {
  209.       if (gp != null) gp.Rotate(false);
  210.     }
  211.  
  212.     public void GoDown()
  213.     {
  214.       if (gp != null) gp.StepDown();
  215.     }
  216.  
  217.     public void Drop()
  218.     {
  219.       if (gp != null) gp.DropDown();
  220.     }
  221.  
  222.     private void TimerEvent(object sender, System.EventArgs e)
  223.     {
  224.       if (gp != null) gp.StepDown();
  225.     }
  226.  
  227.     public void PauseGame()
  228.     {
  229.       MainTimer.Enabled = false;
  230.     }
  231.  
  232.     public void ResumeGame()
  233.     {
  234.       MainTimer.Enabled = true;
  235.     }
  236.  
  237.     public void GameOver()
  238.     {
  239.       MainTimer.Enabled = false;
  240.       FOwner.GameOver();
  241.     }
  242.  
  243.     public void NewGame()
  244.     {
  245.       EmptyPlayingField();
  246.       RowsRemoved = 0;
  247.       CurrentDelay = 500;
  248.       Invalidate();
  249.       gp.init();
  250.       MainTimer.Interval = CurrentDelay;
  251.       MainTimer.Enabled = true;
  252.     }
  253.   }
  254.  
  255.   public class GamePiece: System.Windows.Forms.Panel
  256.   {
  257.     private PlayingField FOwner;
  258.     public int[,] cPiece, nPiece; //current, next
  259.     private MyColors cColor, nColor;
  260.     private int nMaxCols, nMaxRows;
  261.  
  262.     public int cMaxRows, cMaxCols;
  263.  
  264.     public int col, row;
  265.  
  266.     public GamePiece(PlayingField aOwner)
  267.     {
  268.       FOwner = aOwner;
  269.       init();
  270.       init();
  271.     }
  272.  
  273.     public void init()
  274.     {
  275.       int i;
  276.  
  277.       cPiece = nPiece;
  278.       cColor = nColor;
  279.       cMaxCols = nMaxCols;
  280.       cMaxRows = nMaxRows;
  281.  
  282.       col = FOwner.FieldWidth / 2 - 1;
  283.       row = FOwner.FieldHeight;
  284.  
  285.       Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
  286.  
  287.       do {
  288.         i = rdm.Next(1, 9);
  289.         nColor = (MyColors) i;
  290.       } while (cColor == nColor);
  291.  
  292.       switch (rdm.Next(1,8)) {
  293.         case 1:
  294.           //  WW
  295.           //  WW
  296.           nPiece = new int[2,2] {{i,i},{i,i}};
  297.           nMaxCols = 2;
  298.           nMaxRows = 2;
  299.           break;
  300.         case 2:
  301.           //  W
  302.           //  W
  303.           //  W
  304.           //  W
  305.           nPiece = new int[1,4] {{i,i,i,i}};
  306.           nMaxCols = 1;
  307.           nMaxRows = 4;
  308.           break;
  309.         case 3:
  310.           //  W
  311.           //  W
  312.           //  WW
  313.           nPiece = new int [2,3] {{i,i,i},{0,0,i}};
  314.           nMaxCols = 2;
  315.           nMaxRows = 3;
  316.           break;
  317.         case 4:
  318.           //   W
  319.           //   W
  320.           //  WW
  321.           nPiece = new int [2,3] {{0,0,i},{i,i,i}};
  322.           nMaxCols = 2;
  323.           nMaxRows = 3;
  324.           break;
  325.         case 5:
  326.           //  W
  327.           //  WW
  328.           //   W
  329.           nPiece = new int [2,3] {{i,i,0},{0,i,i}};
  330.           nMaxCols = 2;
  331.           nMaxRows = 3;
  332.           break;
  333.         case 6:
  334.           //   W
  335.           //  WW
  336.           //  W
  337.           nPiece = new int [2,3] {{0,i,i},{i,i,0}};
  338.           nMaxCols = 2;
  339.           nMaxRows = 3;
  340.           break;
  341.         default:
  342.           //   W
  343.           //  WW
  344.           //   W
  345.           nPiece = new int [2,3] {{0,i,0},{i,i,i}};
  346.           nMaxCols = 2;
  347.           nMaxRows = 3;
  348.           break;
  349.       }
  350.     }
  351.  
  352.     public void Rotate(bool clockwise)
  353.     {
  354.       int c,r;
  355.       int[,] xPiece = new int[cMaxRows, cMaxCols];
  356.  
  357.       if (clockwise) {
  358.         for (c=0; c<cMaxCols; c++)
  359.           for (r=0; r<cMaxRows; r++)
  360.             xPiece[cMaxRows-1-r,c] = cPiece[c,r];
  361.       }
  362.       else {
  363.         for (c=0; c<cMaxCols; c++)
  364.           for (r=0; r<cMaxRows; r++)
  365.             xPiece[r,cMaxCols-1-c] = cPiece[c,r];
  366.       }
  367.       if (!Overlap(xPiece, cMaxRows, cMaxCols))
  368.       {
  369.         cPiece = xPiece;
  370.         c = cMaxRows;
  371.         cMaxRows = cMaxCols;
  372.         cMaxCols = c;
  373.         FOwner.Invalidate();
  374.       }
  375.     }
  376.  
  377.     public bool Overlap(int[,] piece, int MaxCols, int MaxRows)
  378.     {
  379.       int c;
  380.  
  381.       if ((col < 0) || (row < 0)) return true;
  382.       if (col + MaxCols > FOwner.FieldWidth) return true;
  383.  
  384.       for (int i = 0; i < MaxCols; i++)
  385.         for (int j = 0; j < MaxRows; j++)
  386.           if (row + j < FOwner.FieldHeight) {
  387.             c = piece[i,j];
  388.             if ((c>0) && (FOwner.pfmatrix[i+col,j+row]>0)) return true;
  389.           }
  390.       return false;
  391.     }
  392.  
  393.     public void DropDown()
  394.     {
  395.       do {
  396.         row--;
  397.       } while (!Overlap(cPiece, cMaxCols, cMaxRows));
  398.       row++;
  399.       ConsolidatePiece();
  400.       FOwner.Invalidate();
  401.     }
  402.  
  403.     public void StepDown()
  404.     {
  405.       row--;
  406.       if (Overlap(cPiece, cMaxCols, cMaxRows))
  407.       {
  408.         row++;
  409.         ConsolidatePiece();
  410.       }
  411.       FOwner.Invalidate();
  412.     }
  413.  
  414.     public void StepLeft()
  415.     {
  416.       col--;
  417.       if (Overlap(cPiece, cMaxCols, cMaxRows))
  418.         col++;
  419.       else
  420.         FOwner.Invalidate();
  421.     }
  422.  
  423.     public void StepRight()
  424.     {
  425.       col++;
  426.       if (Overlap(cPiece, cMaxCols, cMaxRows))
  427.         col--;
  428.       else
  429.         FOwner.Invalidate();
  430.     }
  431.  
  432.     public void ConsolidatePiece()
  433.     {
  434.       int count = 0;
  435.  
  436.       if (row + cMaxRows > FOwner.FieldHeight)
  437.       {
  438.         FOwner.GameOver();
  439.         return;
  440.       }
  441.  
  442.       for (int c = 0; c < FOwner.FieldWidth; c++)
  443.         for (int r = 0; r < FOwner.FieldHeight; r++)
  444.           if ((row <= r) && (r < row+cMaxRows) &&
  445.               (col <= c) && (c < col+cMaxCols))
  446.               {
  447.                 FOwner.pfmatrix[c,r] |= cPiece[c-col,r-row];
  448.                 if (cPiece[c-col,r-row] != 0) count++;
  449.               }
  450.  
  451.       FOwner.RemoveRows(count);
  452.       FOwner.Invalidate();
  453.  
  454.       init();
  455.     }
  456.   }
  457. }
  458.